package com.xzx;

import org.apache.velocity.Template;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;
import org.apache.velocity.app.VelocityEngine;

import java.io.FileWriter;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Properties;

/**
 * @author xinzhixuan
 * @version 1.0
 * @date 2024/12/16 17:28
 */
public class Main {
    public static void main(String[] args) throws IOException {
        // 设置velocity资源加载器
        // RuntimeInstance类为整个Velocity渲染提供了一个单例模式，拿到了这个实例就可以完成渲染过程了
        Properties properties = new Properties();
        properties.put("file.resource.loader.class", "org.apache.velocity.runtime.resource.loader.ClasspathResourceLoader");
        Velocity.init(properties);


        //创建Velocity容器
        VelocityContext velocityContext = new VelocityContext();
        velocityContext.put("name", "张三");

        // 加载模板
        Template template = Velocity.getTemplate("vms/hello.vm", StandardCharsets.UTF_8.name());
        FileWriter fw = new FileWriter("/Users/xinzhixuan/work/git2/velocity1234/helloworld/src/main/resources/hello.html");
        //合并数据到模板
        template.merge(velocityContext, fw);
        fw.close();

    }
}